import { _decorator, Component, Node,Label,Sprite ,SystemEvent, systemEvent,EventTouch,tween,Vec3,v3,Color, color,instantiate,Tween,UITransform,Rect,Vec2} from 'cc';
import Util from "./Util";
import {GameMain} from "./GameMain";
import {GameConfig, LineColors, StripColors} from "./GameConfig";
import {Ring} from "./Ring";
import {AudioClipID, AudioManager} from "./AudioManager";
import Platform from "./Platform";
import {LevelConditionBall} from "./LevelConditionBall";
import {BrickParticleManager} from "./BrickParticleManager";
const { ccclass, property } = _decorator;

@ccclass('Ball')
export class Ball extends Component {

    public gridIdx:number=0;

    //val同时也代表颜色id
    public val:number=0;

    //用于画线玩法
    public lineConnected:boolean=false;

    public ballScaleNode:Node=null;
    public ballSprite:Sprite=null;
    public circleSprite:Sprite=null;

    public inited:boolean=false;

    public fadeOut:boolean=false;
    public a:number=255;

    public attachNode:Node=null;


    //以下变量用于画线连接玩法
    //ball rect范围
    public rect:Rect=new Rect(0,0,0,0);
    public uiTrans:UITransform=null;

    public static readonly lineGameScale:number=0.85;

    onLoad()
    {
        this.init();
        //this.addEvent();
    }

    public setLineConnected(connected:boolean)
    {
        this.lineConnected=connected;
    }

    init()
    {
        if(this.inited)
            return;
        this.ballScaleNode=this.node.getChildByName("scale");
        this.ballSprite=this.ballScaleNode.getChildByName("ballSprite").getComponent(Sprite);
        this.circleSprite=this.node.getChildByName("circle").getComponent(Sprite);
        this.attachNode=this.node.getChildByName("attach");
        this.circleSprite.node.active=false;

        this.uiTrans=this.node.getComponent(UITransform);
        this.initRect();

        this.inited=true;
    }

    //用于画线连接玩法
    public initRect(gridWidth:number=GameConfig.GridWidth)
    {

        let w:number=this.uiTrans.width * Ball.lineGameScale;
        w=70;
        let pos:Vec3=this.node.getPosition();
        this.rect.x=pos.x - w*0.5;
        this.rect.y=pos.y -w*0.5;
        this.rect.width=60;
        this.rect.height=60;
    }

    public isInRect(p:Vec2):boolean
    {
        let w:number=70;
        let pos:Vec3=this.node.getPosition();
        this.rect.x=pos.x - w*0.5;
        this.rect.y=pos.y -w*0.5;
        this.rect.width=w;
        this.rect.height=w;

        return this.rect.contains(p);
    }

    public setGridIdx(idx:number)
    {
        this.gridIdx=idx;
        if(this.inited===false)
            this.init();
    }

    public setVal(v:number)
    {
        if(this.inited===false)
            this.init();
        this.val=v;
        if(GameMain.instance)
            this.ballSprite.spriteFrame=GameMain.instance.getBallSpriteFrame(this.val);

        this.circleSprite.color=this.getLineColor();
    }

    public getLineColor():Color
    {
        return LineColors[this.val];
    }

    public getStripColor():Color
    {
        return StripColors[this.val];
    }

    public onPressed()
    {
        /*
        tween(this.node).to(0.07, {scale:v3(1.3, 1.3, 1)}).to(0.8,{scale:Vec3.ONE}, {easing:"elasticOut"}).call(()=>{

        }).start();*/
        this.startScaleAnim();
        this.circleSprite.node.active=true;
    }

    public onPressedInLineGame()
    {
        tween(this.ballScaleNode).to(0.07, {scale:v3(1.3, 1.3, 1)}).to(0.7,{scale:Vec3.ONE}, {easing:"elasticOut"}).call(()=>{

        }).start();
        BrickParticleManager.instance.createNewBlastEffect(this.attachNode, this.getLineColor());
    }

    public onConnect()
    {
        /*
        tween(this.node).to(0.07, {scale:v3(1.3, 1.3, 1)}).to(0.8,{scale:Vec3.ONE}, {easing:"elasticOut"}).call(()=>{

        }).start();*/
        this.startScaleAnim();
        this.circleSprite.node.active=true;
    }

    public onRelease()
    {
        this.circleSprite.node.active=false;
        this.stopScaleAnim();
    }

    public startScaleAnim()
    {
        tween(this.ballScaleNode).to(0.25, { scale: v3(1.2, 1.2, 1)}).to(0.25, { scale: v3(0.9, 0.9,1)}).union().repeatForever().start();
    }

    public stopScaleAnim()
    {
        Tween.stopAllByTarget(this.ballScaleNode);
        this.ballScaleNode.scale=Vec3.ONE;
    }

    public startDestroy()
    {

    }

    public scaleFadeOut()
    {
        if(this.fadeOut)
            return;
        this.fadeOut=true;

        tween(this.ballScaleNode).to(0.3, {scale: v3(0.1, 0.1, 1)}).call(() =>
        {
            this.destroySelf();
        }).start();
    }

    //淡出
    public startFadeOut(conditionBall:LevelConditionBall=null)
    {
        if(this.fadeOut)
            return;
        this.fadeOut=true;
        Tween.stopAllByTarget(this.ballScaleNode);
        //Platform.vibrateShort();


        if(conditionBall!==null)
        {
            tween(this.ballScaleNode).to(0.3, {scale: v3(0.4, 0.4, 1)}).call(() =>
            {
                //let conditionBall: LevelConditionBall = GameMain.instance.mainUI.getConditionBallByColorId(this.val);
                //if (conditionBall !== null)
                {
                    let dstWorldPos: Vec3 = conditionBall.node.getComponent(UITransform).convertToWorldSpaceAR(Vec3.ZERO);
                    let dstLocalPos: Vec3 = GameMain.instance.ballAttachNode.getComponent(UITransform).convertToNodeSpaceAR(dstWorldPos);

                    tween(this.node).to(0.75, {position: dstLocalPos}, {easing: "backIn"}).call(() =>
                    {
                        this.node.removeFromParent(true);
                        this.node.destroy();

                        conditionBall.scaleAnim();
                    }).start();
                }

            }).start();
        }
        else
        {
            tween(this.ballScaleNode).to(0.3, {scale: v3(0.1, 0.1, 1)}).call(() =>
            {
                this.node.removeFromParent(true);
                this.node.destroy();
            }).start();
        }
        //光圈动画
        let ring=instantiate(GameMain.instance.ringPrefab);
        let ringComponent:Ring=ring.getComponent("Ring");
        GameMain.instance.ballAttachNode.addChild(ring);
        ring.setPosition(this.node.getPosition());
        ringComponent.startAnim(this.getLineColor());

        let explode=instantiate(GameMain.instance.ballExplodePrefab);
        GameMain.instance.effectAttachNode.addChild(explode);
        explode.setPosition(this.node.getPosition());
        AudioManager.playClip(AudioClipID.Explode);
    }

    private destroySelf()
    {
        GameMain.instance.grids[this.gridIdx].setBall(null);
        this.node.removeFromParent(true);
        this.node.destroy();
    }

    update(deltaTime: number)
    {

    }
}

